

<?php

//$doc = JFactory::getDocument();
//$doc->addScript('components/com_popup/public/js/jquery.js');

$usertype = $this->usertype;
$mailgroup = $this->mailgroup;
$area = $this->area;
$pref = $this->pref;
$rows = $this->rows;
?>
<div class="popup">
   <form action="<?php echo JRoute::_('index.php?option=com_popup&tmpl=component');?>" name="adminForm_popup" id="adminForm_popup" class="form-popup recipients" method="post">
      <!-- filter -->
      <div id="filter">
         <div class="row">
            <p class="col">
               <label>ユーザタイプ</label>
               <select name="usertype" id="usertype" class="medium">
               <!-- <option value="all" >全ユーザ種別</option> -->
               <option value="office" >本部ユーザ</option>
               <option value="center" >センターユーザ</option>
               <option value="club" >クラブユーザ</option>
               <!--<option value="cooh" >指導員</option> -->
               <option value="group">全メールグループ</option>
               </select>
            </p>

         </div>
         <div class="row">
            <p class="col">
               <label>地区</label>
               <select name="area" id="area" class="medium">
                  <option value="0" selected="selected">全て</option>
                  <?php
                    $area_search = $this->area_search;
                    foreach($area as $value){
                  ?>
                  <option value="<?php echo $value->AREA_CODE; ?>" <?php $temp = isset($area_search) ? $area_search : false;if ($temp == $value->AREA_CODE) { echo ' selected="selected"';} ?> >
                  <?php echo $value->AREA_NAME; ?></option>
                  <?php
                    }
                  ?>
               </select>
            </p>
            <p class="col">
               <label>都道府県</label>
               <span id="area_pref">
                   <select name="pref" id="pref" class="medium">
                      <option value="0" selected="selected">全て</option>
                      <?php
                        $pref_search = $this->pref_search;
                        foreach($pref as $value){
                      ?>
                      <option value="<?php echo $value->PREF_CODE; ?>" <?php $temp = isset($pref_search) ? $pref_search : false;if ($temp == $value->PREF_CODE) { echo ' selected="selected"';} ?> ><?php echo $value->PREF_NAME; ?></option>
                      <?php
                        }
                      ?>
                   </select>
               </span>
            </p>
         </div>
         <div class="row">
            <p class="col">
               <label>センター．クラブ名</label>
               <input class="big" type="text" name="ocba_name" id="ocba_name" value="<?php echo $this->ocba_name; ?>" />
            </p>
            <p class="col right">
               <button type="button" id="btnfind"><?php echo JText::_('検索'); ?></button>
	           <button type="button" id="btnclear" ><?php echo JText::_('クリア'); ?></button>

            </p>
         </div>
      </div>
      <!-- end filter -->
      <!-- table -->
      <div class="clearfix"></div>
      <div id="the_wrap">
          <div class="the_scroll">
              <table>
                 <thead>
                    <tr>
                        <th class="left check"><input id="toggle" name="toggle" style="margin-top: 8px;" type="checkbox" onclick="checkAll(this);checkID();" /></th>
                        <th class="left code hidden">配信先コード</th>
                        <th class="left mail">配信先名</th>
                    </tr>
                 </thead>

                 <tbody>
                 <?php
                 if($rows != null){
                    foreach($rows as $value){
                        if($value->u_id != null){
                 ?>
                    <tr>
                       <td class="center">
                            <input class="checkbox-id" type="checkbox" id="cb<?php echo $value->u_id; ?>" name="cid[]" value="<?php echo $value->u_id; ?>" onclick="isChecked(this.checked);checkID();" />
                       </td>
                       <td class="hidden" id="username<?php echo $value->u_id; ?>"><?php echo $value->username; ?></td>
                       <td id="user<?php echo $value->u_id; ?>"><?php echo $value->name; ?></td>

                    </tr>
                 <?php
                        }
                    }
                 }else{
                 ?>
                    <tr>
                        <td class="center" colspan="3">データは見つかりませんでした</td>
                    </tr>
                 <?php
                    }
                 ?>
                 </tbody>
              </table>
          </div>
          <input type="hidden" name="task" value="" />
          <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
          <textarea name="list_uid" id="list_uid"></textarea>
      </div>
      <!--<a href="#" onclick="if (document.adminForm_popup.boxchecked.value==0){alert('まず、リストから選択してください。');}">Check</a>-->

   </form>
</div>

<script>
    function checkAll(a,b){b||(b="cb");if(a.form){for(var c=0,d=0,f=a.form.elements.length;d<f;d++){var e=a.form.elements[d];if(e.type==a.type&&(b&&0==e.id.indexOf(b)||!b))e.checked=a.checked,c+=!0==e.checked?1:0}if(a.form.boxchecked)a.form.boxchecked.value=c;return!0}for(var e=document.adminForm_popup,c=e.toggle.checked,f=a,g=0,d=0;d<f;d++){var h=e[b+""+d];if(h)h.checked=c,g++}document.adminForm_popup.boxchecked.value=c?g:0}
    function isChecked(a,b){"undefined"===typeof b&&(b=document.getElementById("adminForm_popup"));!0==a?b.boxchecked.value++:b.boxchecked.value--};
	jq(document).ready(function(){

       jq('#usertype').change(function(event){
            if(jq('#usertype').val()=='group'){
                jq('#area').attr('disabled', 'disabled');
                jq('#pref').attr('disabled', 'disabled');
                jq('#ocba_name').attr('disabled', 'disabled');
            } else {
                jq('#area').removeAttr('disabled');
                jq('#pref').removeAttr('disabled');
                jq('#ocba_name').removeAttr('disabled');
            }

       });

		jq("#area").change(function(){

			var id = document.getElementById("area").value;
			var url = "index.php?option=com_popup&task=selectArea&area_code="+id;

            jq.get(url, function(data) {
              jq('#area_pref').html(data);
            });

		})

        //find
        jq("#btnfind").click(function(){
            if(jq('#usertype option:selected').val()!='group'){
                jq.ajax({
                  url: 'index.php?option=com_popup&task=search_user_frmessage'
                    +'&usertype_search='+jq('#usertype option:selected').val()
                    +'&area_search='+jq('#area option:selected').val()
                    +'&pref_search='+jq('#pref option:selected').val()
                    +'&ocba_name='+encodeURI(jq('#ocba_name').val()),
                  context: document.body
                }).done(function(data ) {
                  jq('#the_wrap').html(data);
                });
            } else {
                jq.ajax({
                  url: 'index.php?option=com_popup&task=search_group_frmessage',
                  context: document.body
                }).done(function(data ) {
                  jq('#the_wrap').html(data);
                });
            }



        })
        //clear
        jq("#btnclear").click(function(){
            var usertype_search = 0;
            var mailgroup_search = 0;
            var area_search = 0;
            var pref_search = 0;
            var ocba_name = 0;
            jq("#usertype").val(0);
            jq("#mailgroup").val(0);
            jq("#area").val(0);
            jq("#pref").val(0);

            jq("#ocba_name").val(null);
            jq("#the_wrap").html(
                '<div class="the_scroll">'+
                  '<table>'+
                     '<thead>'+
                        '<tr>'+
                            '<th class="check left"><input style="margin-top: 8px;" id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>'+
                            '<th class="code left hidden">送信先コード</th>'+
                            '<th class="mail left">送信先名</th>'+
                        '</tr>'+
                     '</thead>'+
                     '<tbody>'+
                        '<tr>'+
                            '<td class="center" colspan="3">データは見つかりませんでした</td>'+
                        '</tr>'+
                     '</tbody>'+
                  '</table>'+
              '</div>'+
              '<input type="hidden" name="task" value="" />'+
              '<input type="hidden" name="boxchecked" id="boxchecked" value="0"/>'+
              '<textarea name="list_uid" id="list_uid"></textarea>'
            );

        })


	});

    function checkID(){

        var str = '';
        jq( '.checkbox-id' ).each(function() {
            if(jq(this).is(':checked')){
                if(jq('#usertype option:selected').val()!='group'){
                    id = jq(this).val();
                    //alert(id);
                    str += jq('#user'+id).html()+'('+jq('#username'+id).html()+');';
                } else {
                    id = jq(this).val();
                    //alert(id);
                    str +='*'+jq('#g_name'+id).html()+'('+jq('#g_code'+id).html()+');';
                }

            }
        });

        // set hidden text
        jq('#list_uid').html(str);

    }

</script>
<?php

$app = &JFactory::getApplication();
$app->close();

?>